package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.adapter.MovielistAdapterTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SearchActivityTing extends AppCompatActivity {

    private RelativeLayout llBarTing;
    private ImageView ivBackTing;
    private TextView tvTitleTing;
    private EditText etSearchinputTing;
    private ImageView ivQingkongTing;
    private Button btnSearchTing;
    private Button btnMovienameTing;
    private Button btnCastnameTing;
    private Button btnMovietypeTing;
    private GridView searchListTing;
    private String searchTextTing;
    private RelativeLayout rlSearchWuTing;
    private MovielistAdapterTing movielistAdapterTing;
    private Integer searchTypeTing = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_ting);
        initView();
    }

//    初始化控件
    private void initView() {
        llBarTing = (RelativeLayout) findViewById(R.id.ll_bar_ting);
        llBarTing.setBackgroundColor(Color.parseColor("#1d2d45"));
        ivBackTing = (ImageView) findViewById(R.id.iv_back_ting);
        tvTitleTing = (TextView) findViewById(R.id.tv_title_ting);
        tvTitleTing.setText("");
        etSearchinputTing = (EditText) findViewById(R.id.et_searchinput_ting);
        ivQingkongTing = (ImageView) findViewById(R.id.iv_qingkong_ting);
        btnSearchTing = (Button) findViewById(R.id.btn_search_ting);
        btnMovienameTing = (Button) findViewById(R.id.btn_moviename_ting);
        btnCastnameTing = (Button) findViewById(R.id.btn_castname_ting);
        btnMovietypeTing = (Button) findViewById(R.id.btn_movietype_ting);
        rlSearchWuTing = (RelativeLayout) findViewById(R.id.rl_search_wu_ting);
        searchListTing = (GridView) findViewById(R.id.search_list_ting);
//        返回按钮
        ivBackTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchActivityTing.this.finish();
            }
        });
//        搜索按钮
        btnSearchTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(etSearchinputTing.getText().toString().trim())){
                    new AestheticDialog.Builder(SearchActivityTing.this, DialogStyle.TOASTER, DialogType.WARNING)
                            .setTitle("WARNING")
                            .setMessage("请输入要查找的内容")
                            .setDuration(1500)
                            .show();
                }else {
                    search_btn();
                }

            }
        });
//        清空文本框
        ivQingkongTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearchinputTing.setText("");
            }
        });
//        电影名字按钮获取焦点事件
        btnMovienameTing.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    searchTypeTing = 1;
                }else {
                    searchTypeTing = 0;
                }
            }
        });
//        演员名字按钮获取焦点事件
        btnCastnameTing.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    searchTypeTing = 2;
                }else {
                    searchTypeTing = 0;
                }
            }
        });
//        电影类型按钮获取焦点事件
        btnMovietypeTing.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    searchTypeTing = 3;
                }else {
                    searchTypeTing = 0;
                }
            }
        });
    }

//    获取文本框输入的内容
    private void getEditString(){
        searchTextTing = etSearchinputTing.getText().toString().trim();
    }

//    请求搜索的接口
    private void search_btn() {
        getEditString();
        JsonObject jo = new JsonObject();
        jo = search_type_btn(searchTypeTing);
        OkhttpUtilsTing.isPOST("movie/getMovies", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<MovieBeanTing>> r = gson.fromJson(res, new TypeToken<DataBeanTing<List<MovieBeanTing>>>(){}.getType());
                SearchActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(r.data.size() == 0){
                            rlSearchWuTing.setVisibility(View.VISIBLE);
                            searchListTing.setVisibility(View.GONE);
                        }else {
                            rlSearchWuTing.setVisibility(View.GONE);
                            searchListTing.setVisibility(View.VISIBLE);
                            movielistAdapterTing = new MovielistAdapterTing(SearchActivityTing.this, r.data);
                            searchListTing.setAdapter(movielistAdapterTing);
                        }
                    }
                });
            }
        });
    }

//    获取点击了哪个关键词的按钮
    private JsonObject search_type_btn(Integer i){
        JsonObject jo = new JsonObject();
        if(searchTypeTing == 1){
            jo.addProperty("movieName",searchTextTing);
        }else if(searchTypeTing == 2){
            jo.addProperty("cast",searchTextTing);
        }else if(searchTypeTing == 3){
            jo.addProperty("type",searchTextTing);
        }else{
            jo.addProperty("cast",searchTextTing);
            jo.addProperty("type",searchTextTing);
            jo.addProperty("movieName",searchTextTing);
        }
        return jo;
    }

}